#include "qprogressbarhost.h"

#include "qcommonstruct.h"
#include "qabstractproperty.h"
#include "qpropertyfactory.h"

#include <QProgressBar>

using namespace SHAREDLIB;

QProgressBarHost::QProgressBarHost(QObject *object):
    QWidgetHost(object)
{
    QAbstractProperty *pro;

    pro=QPropertyFactory::createProperty("Number");
    if(pro!=NULL)
    {
        pro->setAttribute("name","value");
        pro->setAttribute("property_name",tr("Value"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Number");
    if(pro!=NULL)
    {
        pro->setAttribute("name","maximum");
        pro->setAttribute("property_name",tr("Maximum"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Number");
    if(pro!=NULL)
    {
        pro->setAttribute("name","minimum");
        pro->setAttribute("property_name",tr("Minimum"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Enum");
    if(pro!=NULL)
    {
        pro->setAttribute("name","alignment");
        pro->setAttribute("property_name",tr("Alignment"));
        pro->setAttribute("group","Attributes");
        ComboItems items;
        tagComboItem item;

        item.m_text=tr("AlignLeft");
        item.m_value=Qt::AlignLeft;
        items.append(item);

        item.m_text=tr("AlignHCenter");
        item.m_value=Qt::AlignHCenter;
        items.append(item);

        QVariant v;
        v.setValue<ComboItems>(items);
        pro->setAttribute("items",v);
        pro->setValue(Qt::AlignLeft);
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Enum");
    if(pro!=NULL)
    {
        pro->setAttribute("name","orientation");
        pro->setAttribute("property_name",tr("Orientation"));
        pro->setAttribute("group","Attributes");
        ComboItems items;
        tagComboItem item;

        item.m_text=tr("Horizontal");
        item.m_value=Qt::Horizontal;
        items.append(item);

        item.m_text=tr("Vertical");
        item.m_value=Qt::Vertical;
        items.append(item);

        QVariant v;
        v.setValue<ComboItems>(items);
        pro->setAttribute("items",v);
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("ByteArray");
    if(pro!=NULL)
    {
        pro->setAttribute("name","format");
        pro->setAttribute("property_name",tr("Format"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Bool");
    if(pro!=NULL)
    {
        pro->setAttribute("name","invertedAppearance");
        pro->setAttribute("property_name",tr("InvertedAppearance"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }


    pro=QPropertyFactory::createProperty("Bool");
    if(pro!=NULL)
    {
        pro->setAttribute("name","textVisible");
        pro->setAttribute("property_name",tr("TextVisible"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }


    pro=QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","valueChanged");
        pro->setAttribute("property_name",tr("ValueChanged"));
        pro->setAttribute("group","Events");
        m_object->setProperty("valueChanged","");
        addProperty(pro);
    }

    appendMemery("value");
    appendMemery("textVisible");

    QProgressBar* gre=(QProgressBar*)m_object;
    connect(gre,SIGNAL(valueChanged(int)),this,SLOT(valueChanged(int)));
}

void QProgressBarHost::propertyChanged()
{
    QWidgetHost::propertyChanged();
    if(m_ready)
    {
        SHAREDLIB::QAbstractProperty    *pro=(SHAREDLIB::QAbstractProperty*)sender();
        if(pro->attribute("name").toString()=="orientation")
        {
            QRect re=getPropertyValue("geometry").toRect();
            re.setRect(re.x(),re.y(),re.height(),re.width());
            setPropertyValue("geometry",re);
        }
    }
}

void QProgressBarHost::setValue(int value)
{
    setPropertyValue("value",value);
}

int QProgressBarHost::value()
{
    return getPropertyValue("value").toInt();
}

void QProgressBarHost::setAlignment(int alignment)
{
    setPropertyValue("alignment",alignment);
}

int QProgressBarHost::alignment()
{
    return getPropertyValue("alignment").toInt();
}

void QProgressBarHost::setOrientation(int orientation)
{
    setPropertyValue("orientation",orientation);
}

int QProgressBarHost::orientation()
{
    return getPropertyValue("orientation").toInt();
}

void QProgressBarHost::setFormat(const QString &format)
{
    setPropertyValue("format",format);
}

QString QProgressBarHost::format()
{
    return getPropertyValue("format").toString();
}

void QProgressBarHost::setInvertedAppearance(bool invertedAppearance)
{
    setPropertyValue("invertedAppearance",invertedAppearance);
}

bool QProgressBarHost::invertedAppearance()
{
    return getPropertyValue("invertedAppearance").toBool();
}

void QProgressBarHost::setTextVisible(bool textVisible)
{
    setPropertyValue("textVisible",textVisible);
}

bool QProgressBarHost::textVisible()
{
    return getPropertyValue("textVisible").toBool();
}

void QProgressBarHost::valueChanged(int value)
{
    QString code=getPropertyValue("valueChanged").toString();
    if(code!="")
    {
        QMap<QString,QString> param;
        param.insert("value",QString::number(value));
        emit_exec(code,param);
    }
}
